package com.bootdo.common.config;

import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method; 

/**
 * @author Administrator
 *自定义注解 + aop方式实现数据源动态切换
 */
@Aspect  
@Component  
public class DynamicDataSourceAspect {
	  
    @Before("@annotation(DS)")  
    @Order
    public void beforeSwitchDS(JoinPoint point){  
        //获得当前访问的class  
        Class<?> className = point.getTarget().getClass();  
        //获得访问的方法名  
        String methodName = point.getSignature().getName();  
        //得到方法的参数的类型  
        Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();  
        String dataSource = DataSourceContextHolder.DEFAULT_DS;  
        try {  
            // 得到访问的方法对象  
            Method method = className.getDeclaredMethod(methodName, argClass);  
            // 判断是否存在@DS注解  
            if (method.isAnnotationPresent(DS.class)) {  
                DS annotation = method.getAnnotation(DS.class);  
                // 取出注解中的数据源名  
                dataSource = annotation.value();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        // 切换数据源  
        DataSourceContextHolder.setDB(dataSource);  
    }  
      
    @After("@annotation(DS)")  
    public void afterSwitchDS(JoinPoint point){  
        DataSourceContextHolder.clearDB();  
    }  
}
